package com.kkang.netpan.web;

import com.kkang.netpan.entity.FileDto;
import com.kkang.netpan.service.HiveJDBC;
import com.kkang.netpan.service.NetpanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
public class NetpanController {

    @Autowired
    NetpanService service;

    @Autowired
    HiveJDBC hiveJDBC ;

/*    @RequestMapping("/get")
    public List<FileDto> getFile(@RequestParam("pwd") String pwd  ,
                                        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin" , "*");
        File[] fileOrDir = service.scanFileOrDir(pwd);
        List<FileDto> filelist  = new LinkedList<>();
        for (File file : fileOrDir) {
            filelist.add(new FileDto(file.getName() , file.length() , file.isFile()?"1":"0"));
        }
        return filelist ;
    }

    @RequestMapping("/testhive")
    public String getTablesName() throws Exception {
        return hiveJDBC.getTables();
    }*/

    @RequestMapping("/map")
    public List<Map<String , Object>> resultMap(HttpServletResponse response,
                                                @RequestParam("sql") String sql) {
        response.setHeader("Access-Control-Allow-Origin" , "*");
        return hiveJDBC.executor(sql);
    }

}
